<template>
  <div id="keepDataTopSearch">
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="" prop="save">
          <el-select v-model="formInline.save" placeholder="请选择">
            <el-option
              v-for="item in saveList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-select
            v-model="formInline.source"
            placeholder="请选择来源"
            clearable
          >
            <el-option
              v-for="item in sourceList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="统计时间" prop="timeType">
          <el-select v-model="formInline.timeType" placeholder="请选择">
            <el-option
              v-for="item in timeTypeList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="createTime">
          <el-date-picker
            v-model="formInline.createTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd"
            :disabled="canChoose"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sysNormalDisable } from '@utils/dictionariesFunc'
import { getCreateDate } from '@utils/pubFunction'
import { dateRangeMixins } from '@/mixins/dateRange'
export default {
  name: 'keepDataTopSearch',
  msg: '留存分析-顶部搜索',
  mixins: [dateRangeMixins],
  props: {
    formInline: {
      type: Object,
      default: function() {
        return {
          // 搜索条件
          save: '', //留存
          source: '', //来源
          timeType: '', //统计时间
          startDate: '', // 开始时间
          endDate: '', // 结束时间
          createTime: []
        }
      }
    }
  },
  data() {
    return {
      canChoose: true, //时间是否不可选
      rules: {
        save: {},
        timeType: {},
        source: {},
        createTime: {
          type: 'array',
          required: true,
          message: '统计时间不能为空'
        }
      },
      saveList: [
        // 留存
        { dictLabel: '留存率', dictValue: 'rate' },
        { dictLabel: '留存数', dictValue: 'num' }
      ],
      timeTypeList: [
        // 统计时间
        { dictLabel: '最近7天', dictValue: '7' },
        { dictLabel: '最近30天', dictValue: '30' },
        { dictLabel: '自定义', dictValue: 'auto' }
      ],
      sourceList: [
        // 来源
        { dictLabel: '全部', dictValue: '' }
      ]
    }
  },
  watch: {
    'formInline.createTime': {
      handler(newVal, oldVal) {
        if (newVal && newVal.length > 1) {
          this.formInline.startDate = newVal[0]
          this.formInline.endDate = newVal[1]
        } else {
          this.formInline.startDate = ''
          this.formInline.endDate = ''
        }
      },
      deep: true
    },
    'formInline.timeType': {
      handler(newVal, oldVal) {
        if (newVal === 'auto') {
          //自定义
          /* this.formInline.createTime = []*/
          this.canChoose = false
        } else {
          this.getCreateDate('Date', newVal)
          this.canChoose = true
        }
      },
      deep: true
    }
  },
  mounted() {
    this.getStatusList()
  },
  methods: {
    /* 获取字典状态 */
    async getStatusList() {
      let data = await sysNormalDisable('dataview_source')
      this.sourceList = this.sourceList.concat(data)
      this.getCreateDate('Date')
      this.setDefaultData()
      this.$emit('toSearch')
    },
    /* 搜索 */
    toSearch(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('toSearch')
        } else {
          return false
        }
      })
    },
    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
      this.setDefaultData()
      this.getCreateDate('Date')
    },
    /*设置默认数据*/
    setDefaultData() {
      this.formInline.save = this.saveList[0].dictValue
      this.formInline.timeType = this.timeTypeList[1].dictValue
    }
  }
}
</script>

<style lang="less"></style>
